﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NAudio.Midi;
using NAudio.Wave;
using System.Diagnostics;
using PT.NoteDetection.Envelope;
using PT.NoteDetection.Midi;

namespace PT.NoteDetection.Tests.Envelope {
    [TestClass]
    public class EnvelopeNoteDetectorTest {
        [TestMethod]
        public void SingleStroke90Test() {

            var referenceFile = new MidiFile(new MemoryStream(Resources.SingleStroke90evt), true);
            var expectedEvents = referenceFile.Events[1];
            Assert.IsTrue(expectedEvents.Count > 0);

            var expectedNoteEvents = from noteEvent in expectedEvents
                                     where noteEvent is NoteOnEvent
                                     select noteEvent;

            var expectedNoteEnumerator = expectedNoteEvents.GetEnumerator();
            expectedNoteEnumerator.MoveNext();

            var sourceStream = new WaveFileReader(Resources.SingleStroke90sndMonoFloat);
            var detector = new EnvelopeNoteDetector(sourceStream, new TimeService());

            var notes = new List<NoteOnEvent>();
            var allNotes = new List<NoteOnEvent>();

            var timeService = detector.TimeService;
            var sw = Stopwatch.StartNew();
            Trace.WriteLine("Starting to read from stream.", "Test");

            // TODO: fix test
            //while(detector.Read(notes) > 0) {
            //    allNotes.AddRange(notes);
            //    for (var i = 0; i < notes.Count; i++) {
            //        expectedNoteEnumerator.MoveNext();
            //        var expectedNote = expectedNoteEnumerator.Current;

            //        var actualNote = notes[i];

            //        Trace.WriteLine(string.Format("{0, 6} ({1} ms) {2}", actualNote.AbsoluteTime, timeService.CalculateTime(actualNote.AbsoluteTime), actualNote), "Test");
            //    }
            //}

            Trace.WriteLine(string.Format("Processed file in {0} ms.", sw.ElapsedMilliseconds), "Test");
            Trace.Flush();
        }
    }
}